package lowijs.XfireClient.packet;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;

public class FriendStatusTextPacket extends Packet {
	public FriendStatusTextPacket(byte[] data) {
		int numSids = fromLowHigh(data[9], data[10]);
		Friend[] friends = new Friend[numSids];
		FriendManager fm = FriendManager.getInstance();
		int pos = 11;
		
		for (int i = 0; i < numSids; i++) {
			byte[] sid = new byte[16];
			System.arraycopy(data, pos, sid, 0, 16);
			friends[i] = fm.getFriendBySid(sid);
			pos += 16;
		}
		
		pos += 8; // jump to status texts
		
		for (int i = 0; i < numSids; i++) {
			int textLen = fromLowHigh(data[pos], data[pos + 1]);
			byte[] statustext = new byte[textLen];
			System.arraycopy(data, pos + 2, statustext, 0, textLen);
			
			if (friends[i] != null) {
				if (textLen == 0)
					friends[i].setStatusText("Online");
				else
					friends[i].setStatusText(new String(statustext));
			}
			EventManager.fireEvent(new FriendStatusTextEvent(friends[i]));
			pos += textLen + 2;
		}
	}
}
